package unibz.cn.gui;

import java.util.Iterator;
import java.util.List;

import javax.swing.JOptionPane;

import org.dom4j.Node;

import unibz.cn.logic.User;

public class FollowWindow extends javax.swing.JFrame {

    public FollowWindow(User aUser) {
        initComponents(aUser);
        refreshFollowers();
        
    }
    
    /**
     * Print the updated list of the people we are following
     */
    private void refreshFollowers() {
    	try {
			List list = aUser.getFriends();
			Iterator iter=list.iterator();
			while(iter.hasNext()){
				Node node=(Node)iter.next();
				Node nameNode = node.selectSingleNode("screen_name");
				followersTextArea.append(nameNode.getText()+"\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    /**
     * Create the graphical User interface and add the listener to the buttons
     * @param aUser
     */
    private void initComponents(User aUser) {

        jScrollPane1 = new javax.swing.JScrollPane();
        followersTextArea = new javax.swing.JTextArea();
        addButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        followersLabel = new javax.swing.JLabel();
        inputTextField = new javax.swing.JTextField();
        inputLabel = new javax.swing.JLabel();
        backButton = new javax.swing.JButton();
        this.aUser = aUser;

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        
        setTitle("Follow/Unfollow");
        
        followersTextArea.setColumns(20);
        followersTextArea.setRows(5);
        followersTextArea.setEditable(false);
        jScrollPane1.setViewportView(followersTextArea);

        addButton.setText("Add");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });
        
        removeButton.setText("Remove");
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        followersLabel.setText("Followers:");

        inputLabel.setText("Follow/Unfollow");

        backButton.setText("Back");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(followersLabel)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(removeButton))
                        .addComponent(inputLabel)
                        .addComponent(inputTextField))
                    .addComponent(backButton))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(followersLabel)
                    .addComponent(inputLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(inputTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(addButton)
                            .addComponent(removeButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(backButton))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }

    /**
     * Given a name in the inputTextField unfollow it
     * @param evt
     */
    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	String friendName = inputTextField.getText();
    	try {
    		System.out.println(followersTextArea.getText());
    		if (followersTextArea.getText().indexOf(friendName+"\n") >= 0) {
			aUser.unfollow(friendName);
			followersTextArea.setText("");
			refreshFollowers();
    		} else 	JOptionPane.showMessageDialog(null,"You have not a friend with such a name", "Attention",JOptionPane.ERROR_MESSAGE);
		} catch (Exception e) {
			
		}
    }

    /**
     * Given a name in the inputTextField follow it
     * @param evt
     */
    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	String friendName = inputTextField.getText();
    	try {
    		aUser.follow(friendName);
    		followersTextArea.setText("");
    		refreshFollowers();
    	} catch (Exception e) {
		}
    }

    /**
     * close the window
     * @param evt
     */
    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	this.dispose();
    }

    private javax.swing.JButton addButton;
    private javax.swing.JButton removeButton;
    private javax.swing.JButton backButton;
    private javax.swing.JLabel followersLabel;
    private javax.swing.JLabel inputLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea followersTextArea;
    private javax.swing.JTextField inputTextField;
    private User aUser;

}
